//
//  LoginViewController.m
//  YongChuanTradingArea
//
//  Created by Vivian on 2017/9/12.
//  Copyright © 2017年 李冰. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "ForgetPwdViewController.h"
#import "DirectionButton.h"

#import "ApplyIndividualViewController.h"
#import "ApplyResultViewController.h"
#import "ApplyMemberShopViewController.h"
#import "BaseNavigationController.h"
#import "ApplyViewController.h"

@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (weak, nonatomic) IBOutlet UITextField *telTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

#pragma mark - btn clicked
- (IBAction)loginBtnAction:(UIButton *)sender {
    if (self.telTF.text.length == 0) {
        [MBProgressHUD showError:@"请输入账号！"];
        return;
    }
    if (self.codeTF.text.length == 0) {
        [MBProgressHUD showError:@"请输入密码！"];
        return;
    }
    [self requestData];
}


- (IBAction)forgetPwdBtnAction:(UIButton *)sender {
    ForgetPwdViewController *vc = [[ForgetPwdViewController alloc]init];
    [self presentViewController:vc animated:YES completion:^{
        
    }];
}

- (IBAction)registBtnAction:(UIButton *)sender {
    RegisterViewController *vc = [[RegisterViewController alloc]init];
    [self presentViewController:vc animated:YES completion:^{
        
    }];
}

#pragma mark - request data
- (void)requestData{
    [LBNetworkingInstance requestGET:accountLogin parameters:@{@"userName":self.telTF.text,@"password":self.codeTF.text} showHUD:YES success:^(id responseObject, NSInteger code, NSString *message) {
        if (code == YCNetworkingStatusCodeSuccess){
            [LBUserManage defaultManager].userModel = [UserModel yy_modelWithJSON:responseObject[@"Data"]];
            [MBProgressHUD showSuccess:@"登录成功！"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                UserModel * model = [LBUserManage defaultManager].userModel;
                if (([model.TOUSERTYPE isEqualToString:@"1"] || [model.TOUSERTYPE isEqualToString:@"0"]) && [model.USERTYPE isEqualToString:@"1"]) {
                    //            跳转到申请成为会员店或者企业合伙人的页面
                    ApplyViewController * vc = [[ApplyViewController alloc]init];
                    [self toVC:vc];
                } else {
                    if ([model.MEMBERSTATUS isEqualToString:@"-1"]) {
                        if ([model.TOUSERTYPE isEqualToString:@"2"]) {
                            //                    跳转到会员店填写资料页面
                            ApplyMemberShopViewController * vc = [[ApplyMemberShopViewController alloc]init];
                            vc.isMember = YES;
                            [self toVC:vc];
                        } else if ([model.TOUSERTYPE isEqualToString:@"3"]) {
                            //                    跳转到注册企业合伙人填写资料页面
                            ApplyMemberShopViewController * vc = [[ApplyMemberShopViewController alloc]init];
                            vc.isMember = NO;
                            [self toVC:vc];
                        } else if ([model.TOUSERTYPE isEqualToString:@"4"]) {
                            //                    跳转到注册个人合伙人填写资料页面
                            ApplyIndividualViewController * vc = [[ApplyIndividualViewController alloc]init];
                            [self toVC:vc];
                        } else {
                            //                    跳转到申请成为会员店或者企业合伙人的页面
                            ApplyViewController * vc = [[ApplyViewController alloc]init];
                            [self toVC:vc];
                        }
                    } else if ([model.MEMBERSTATUS isEqualToString:@"3"]) {
                        [UIViewController toHome];
                    } else {
                        //                跳转到待审核页面
                        ApplyResultViewController * vc = [[ApplyResultViewController alloc]init];
                        [self toVC:vc];
                    }
                }
            });
        }
    } failure:^(NSError *err) {
        
    }];
}

- (void)toVC:(BaseViewController *)vc{
    BaseNavigationController * nav = [[BaseNavigationController alloc]initWithRootViewController:vc];
    [self presentViewController:nav animated:YES completion:^{
        
    }];
}

- (void)getCode{
    [LBNetworkingInstance requestGET:getVerifyCode parameters:@{@"phoneNumber":self.telTF.text} showHUD:YES success:^(id responseObject, NSInteger code, NSString *message) {
        
    } failure:^(NSError *err) {
        
    }];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    
    return UIStatusBarStyleDefault;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
